<template>
<el-card body-style="padding: 10px" class="file-download-card" shadow="hover" @click="downloadFile">
  <el-container class="container">
    <el-aside class="aside">
      <div class="explanation">文件传输</div>
      <div>{{ fileName }}</div>
    </el-aside>
    <el-main class="main">
      <div class="file-icon-wrapper">
        <img v-if="['jpg', 'jpeg', 'png', 'gif'].includes(ext)" src="@/assets/fileIcons/image.png" alt="file icon" class="file-icon" />
        <img v-else-if="['pdf'].includes(ext)" src="@/assets/fileIcons/pdf.png" alt="file icon" class="file-icon" />
        <img v-else-if="['doc', 'docx'].includes(ext)" src="@/assets/fileIcons/word.png" alt="file icon" class="file-icon" />
        <img v-else-if="['ppt', 'pptx'].includes(ext)" src="@/assets/fileIcons/ppt.png" alt="file icon" class="file-icon" />
        <img v-else-if="['xls', 'xlsx', 'csv'].includes(ext)" src="@/assets/fileIcons/excel.png" alt="file icon" class="file-icon" />
        <img v-else-if="['zip', 'rar', '7z'].includes(ext)" src="@/assets/fileIcons/zip.png" alt="file icon" class="file-icon" />
        <img v-else-if="['txt', 'md'].includes(ext)" src="@/assets/fileIcons/text.png" alt="file icon" class="file-icon" />
        <img v-else src="@/assets/fileIcons/unknown.png" alt="file icon" class="file-icon" />
      </div>
    </el-main>
  </el-container>
</el-card>
</template>

<script>
import {ElMessage} from "element-plus";

export default {
  name: "FileMessageEle",
  props: {
    file: {
      type: String,
    },
    fileName: {
      type: String,
    }
  },
  data() {
    return {
      ext: this.getExt(this.file),
      fileUrl: this.getUrl(this.file),
    };
  },
  methods: {
    getUrl(file) {
      return `https://job-linker.obs.cn-north-4.myhuaweicloud.com/${file}`
    },
    getExt(file) {
      return file.split('.').pop().toLowerCase();
    },
    downloadFile() {
      const link = document.createElement('a');
      link.href = this.fileUrl;
      link.download = this.fileName;
      console.log(link);
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      ElMessage.success('准备下载……');
    }
  }
}
</script>

<style scoped>
.file-download-card {
  width: 350px;
}
.file-download-card:hover {
  cursor: pointer;
}
.container {
  width: 100%;
}
.aside {
  width: 250px;
  overflow: hidden;
}
.main {
  padding: 0;
  overflow: hidden;
}
.file-icon {
  height: 80px;
}
.file-icon-wrapper {
  overflow: hidden;
}
.explanation {
  color: #8492a6;
}
</style>